<?php
/**
 * Created by PhpStorm.
 * Author: kaodou
 * Date: 2017/10/18
 * Time: 14:33
 */
namespace App\Http\Admin\StockOut\Models;

use Framework\BaseClass\Http\Admin\Model;
use App\Eloquent\Admin\StockOut as EloquentStockOut;

class StockOut extends Model
{
    public function getProList()
    {
        return EloquentStockOut::all();
    }

    public function addStockOut($saveData)
    {
        $StockOutInfo = EloquentStockOut::firstOrNew(['product_no' => $saveData['product_no']]);
        if ($StockOutInfo->id) xThrow(ERR_CLIENT_USER_ALREADY_EXISTS);

        return $this->saveStockOut($StockOutInfo, $saveData);
    }

    public function updateStockOut($StockOutId, $saveData)
    {
        $StockOutInfo = EloquentStockOut::find($StockOutId);
        if (!$StockOutInfo) xThrow(ERR_CLIENT_USER_NOT_EXIST);

        return $this->saveStockOut($StockOutInfo, $saveData);
    }

    private function saveStockOut(EloquentStockOut $StockOutInfo, array $saveData)
    {
        $StockOutInfo->fill($saveData);
        return $StockOutInfo->save();
    }
}